package jdbc;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class DBUtil {
    //连接池 DruidDateSource
    private static DruidDataSource ds;

    /*    static {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }*/
    static {
        ds = new DruidDataSource();
//        ds.setDriverClassName("com.mysql.cj.jdbc.Driver");//阿里提供的连接池可以不设置，会自动匹配
        //设置与数据库链接相关的信息
        ds.setUrl("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
        ds.setUsername("root");
        ds.setPassword("byb141700");
        ds.setInitialSize(5);//初始连接数-数据库连接池创建好后内部默认创建的链接数量
        ds.setMaxActive(30);//最大连接数
    }

    public static Connection getConnection() throws SQLException {
/*        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "byb141700");*/
        return ds.getConnection();
    }
}
